//
//  ViewController.m
//  美食百科
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 doll. All rights reserved.
//

#import "ViewController.h"
#import "UIImageView+WebCache.h"
#import "NewWorking.h"
#import "CarouselModel.h"
#import "Reachability.h"
#import "CarouselView.h"
#define kWidth self.view.frame.size.width
#define kHeight self.view.frame.size.height
@interface ViewController ()<UIScrollViewDelegate>
@property(nonatomic,retain)UIScrollView *scrollView;
@property(nonatomic,retain)NSMutableArray *carouselArray;
@property(nonatomic, retain) NSTimer *timer;
@property(nonatomic, assign) BOOL isDragging;
@property(nonatomic, retain) UIPageControl *page;
@property(nonatomic,retain)UIView *leftView;
@end

@implementation ViewController
- (void)dealloc
{
    [_scrollView release];
    [_carouselArray release];
    [_timer release];
    [_page release];
    [_leftView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBarHidden = YES;
    //self.navigationController.navigationBar.barTintColor = [UIColor clearColor];
    self.navigationController.navigationBar.alpha = 0;
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.9 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
    Reachability *reach = [Reachability reachabilityForInternetConnection];
    
    //如果无网络,弹窗提示
    if ( 0 == reach.currentReachabilityStatus){
        //弹窗提示
        UILabel *nonconnectionLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, -50, kWidth, 50)];
        nonconnectionLabel.textAlignment = NSTextAlignmentCenter;
        nonconnectionLabel.backgroundColor = [UIColor redColor];
        nonconnectionLabel.text = @"网络不给力,刷新试试~";
        nonconnectionLabel.textColor = [UIColor whiteColor];
        [self.view addSubview:nonconnectionLabel];
        
        //动画效果,通知栏缓慢出现
        [UIView animateWithDuration:0.6 animations:^{
            nonconnectionLabel.frame = CGRectMake(0, 0, kWidth, 50);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.6 delay:3 options:UIViewAnimationOptionAutoreverse animations:^{
                nonconnectionLabel.frame = CGRectMake(0, -50, kWidth, 50);
                [nonconnectionLabel release];

            } completion:^(BOOL finished) {
                
            }];
            
//            [UIView animateWithDuration:1.9 animations:^{
//                nonconnectionLabel.frame = CGRectMake(0, -50, kWidth, 50);
//            //[nonconnectionLabel removeFromSuperview];
//                [nonconnectionLabel release];
//            }];
            
        }];
    
    }
    });

    
    /*************** 解析轮播图数据 ****************/
    
    self.carouselArray = [NSMutableArray array];
    
    [NewWorking GetDayaWithURL:@"http://food.boohee.com/fb/v1/welcome" dic:nil sussess:^(id responseObject) {
        CarouselModel *carouselModel = [CarouselModel carouselModelWithDictionary:responseObject[@"banner"]];
        
        [self.carouselArray addObject:carouselModel];
        
        for (NSDictionary *dic in responseObject[@"others"]) {
            CarouselModel *carouselModel = [CarouselModel carouselModelWithDictionary:dic];
            
            [self.carouselArray addObject:carouselModel];
        }
        
         //[self creatLunbo];
        NSLog(@"%@",self.carouselArray);
        NSLog(@"%ld",self.carouselArray.count);
        CarouselView *carouselView = [[CarouselView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight/2) array:self.carouselArray];
    
    [self.view addSubview:carouselView];
    [carouselView release];
    } filed:^(NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
//    [self.view addSubview:self.scrollView];
//    
//    [_scrollView release];
    
    
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    leftButton.frame = CGRectMake(15, 25, 20, 20);
    [leftButton setBackgroundImage:[UIImage imageNamed:@"more"] forState:UIControlStateNormal];
    [leftButton addTarget:self action:@selector(viewAppearance:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:leftButton];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
    
    [self.view addGestureRecognizer:tap];
    
    [tap release];
}


-(void)viewAppearance:(UIButton *)button
{
    self.leftView = [[UIView alloc]initWithFrame:CGRectMake(-kWidth / 5 * 4, 0, kWidth / 5 * 4, kHeight)];
     self.leftView.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.leftView];
       [UIView animateWithDuration:0.4 animations:^{
       self.leftView.frame = CGRectMake(0, 0,kWidth / 5 * 4, kHeight);
    }];
    
    
    
    
}

-(void)tap:(UITapGestureRecognizer *)tap
{
    [UIView animateWithDuration:9 animations:^{
        self.leftView.frame = CGRectMake(-kWidth / 5 * 4, 0,kWidth / 5 * 4, kHeight);
    }];
    [self.leftView removeFromSuperview];
}

     


/**********************  状态栏隐藏  **********************/

//	- (UIStatusBarStyle)preferredStatusBarStyle
//{
//    return UIStatusBarStyleLightContent;
//}

- (BOOL)prefersStatusBarHidden{
    return YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
